<?php
    // +----------------------------------------------------------------------
    // | 海豚PHP框架 [ DolphinPHP ]
    // +----------------------------------------------------------------------
    // | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
    // +----------------------------------------------------------------------
    // | 官方网站: http://dolphinphp.com
    // +----------------------------------------------------------------------
    // | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
    // +----------------------------------------------------------------------

    namespace app\member\admin;


    use app\admin\controller\Admin;
    use app\common\builder\ZBuilder;
    use app\member\model\MemberModel;
    use app\base\model\WalletProductModel;
    use app\member\model\MemberOperationGroupModel;

    class Memberoperation extends Admin
    {

        protected $members = [];

        /**
         * @return mixed
         * @throws \think\Exception
         * @throws \think\exception\DbException
         */
        public function index()
        {

            // 查询
            $map = $this->getMap();

            // 排序
            $order = $this->getOrder('m.id asc');
            // 数据列表
            $data_list = MemberOperationGroupModel::getObjectData()->where($map)->order($order)->paginate();
            //                        echo json_encode([$data_list[0]['type'],]);
            //                        exit;


            // 使用ZBuilder快速创建数据表格
            return ZBuilder::make('table')->setSearchArea([
                ['text','id','用户ID'],
                ['text','phone','用户手机号'],
                //                ['daterange','create_time','注册时间','','',['min-date' => '2018-10-10']]
            ])// 设置搜索框
            ->addTopButtons(['add'])// 批量添加顶部按钮
            ->addColumns([ // 批量添加数据列
                           ['id','用户ID'],
                           ['aliplay_name','姓名'],
                           ['phone','手机号码','link',url('member/index/view',['id' => '__member_id__'])],
                           ['money','钱包','text'],
                           ['point','积分','text'],
                           ['num','数量','text'],
                           ['lock_num','锁仓','text'],
                           ['reward_num','奖励','callback','view_money'],

                           ['status', '状态', 'status', '', ['0' => '启用:warning', '1' => '禁用:default']],
                           ['type', '类型', 'status', '', ['0'
                           => '挂单:danger', '1' => '正常:success']],


                           ['right_button','操作','btn']
            ])//
            ->addRightButtons([
                'view' => [
                    'title' => '查看详情',
                    'href'  => url('add',['id' => '__group_id__'])
                ]
            ])// 批量添加右侧按钮
            ->addOrder('phone,money,point,lock_num,reward_num,num')->setRowList($data_list)// 设置表格数据
            ->fetch(); // 渲染模板
        }

        /**
         * 新增
         *
         * @return mixed
         * @throws Exception
         */
        public function add($id = 0)
        {

            // 保存数据
            if ($this->request->isPost())
            {
                // 表单数据
                $data = $this->request->post();

                if (!isset($data['phone']))
                {
                    $this->error('手机号码必填');
                }

                $MemberModel       = MemberModel::where(['phone' => $data['phone']])->find();
                $data['member_id'] = $MemberModel['id'];
                if (empty($MemberModel))
                {
                    $this->error('请填写正确的手机号码');
                }

                if ($id > 0)
                {
                    $return_id = $GoodsModel = MemberOperationGroupModel::update($data,['id' => $id]);
                }
                else
                {

                    $return_id = $GoodsModel = MemberOperationGroupModel::create($data);
                }
                if ($return_id)
                {
                    // 记录行为
                    //action_log('GoodsModel_add', null, $GoodsModel['id'], session('admin_id'), $GoodsModel['id']);
                    $this->success('新增成功','index');
                }
                else
                {
                    $this->error('新增失败');
                }
            }
            $info = MemberOperationGroupModel::field('*')->where(['id' => $id])->find();
            // 显示添加页面
            return ZBuilder::make('form')//
            ->setBtnTitle('back','返回前一页')//
            ->addFormItems([
                ['text','phone','手机号码'],
                ['radio','type','类型','',['挂单'],0],
                ['radio','status','立即启用','',['是','否'],0],
            ])->setFormData($info)//
            ->fetch();
        }
    }